package com.zm.service;

import com.mongodb.client.MongoCollection;
import com.zm.bean.HeartBeatBean;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CabHeartBeatDataServiceImpl implements CabHeartBeatDataService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public List<HeartBeatBean> findAllByCabinet(String collectionName, String date, String cabinet, long startLong, long endLong) {
        return mongoTemplate.find(getQueryByCid(cabinet, startLong, endLong), HeartBeatBean.class, collectionName);
    }

    @Override
    public List<HeartBeatBean> findAllByBid(String collectionName, String date, String bid, int limit) {
        return mongoTemplate.find(getQueryBybid(bid, limit), HeartBeatBean.class, collectionName);
    }

    public MongoCollection<Document> getCollection(String tempDate) {
        return mongoTemplate.getCollection(tempDate);
    }

    //多个条件查询(格式1)
    public Query getQueryByCid(String cid, long startLong, long endLong) {
        Query query = new Query();
        query.addCriteria(Criteria.where("cabinet").is(cid));
        query.addCriteria(Criteria.where("time").gte(startLong).lte(endLong));
        query.with(new Sort(Sort.Direction.ASC, "time"));
        query.with(new Sort(Sort.Direction.ASC, "door"));
        query.limit(2000 * 8);
        return query;
    }

    //多个条件查询(格式1)
    public Query getQueryBybid(String bid, int limit) {
        Query query = new Query();
        query.addCriteria(Criteria.where("battery").is(bid));
        query.with(new Sort(Sort.Direction.ASC, "time"));
        query.limit(limit);
        return query;
    }

}
